package com.example.dts.controller;

import com.example.dts.controller.base.BaseController;
import com.example.dts.controller.base.OR;
import com.example.dts.model.ro.UserRO;
import com.example.dts.model.vo.LoginVO;
import com.example.dts.model.vo.UserVO;
import com.example.dts.service.UserService;
import com.example.dts.utils.DtsException;
import io.lettuce.core.ScriptOutputType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Api(value = "用户管理", tags = {"用户管理"})
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    /**
     * 注册
     *
     * @param ro
     * @return
     */
    @PostMapping(value = "/registry")
    @ApiOperation(value = "用户注册")
    public OR<Void> add(@RequestBody UserRO ro) {
        userService.add(ro);
        System.out.println("注册");
        return successResult();
    }

    /**
     * 登录
     *
     * @param ro
     * @return
     */
    @PostMapping(value = "/login")
    @ApiOperation(value = "用户登录")
    public ResponseEntity<OR<LoginVO>> find(@RequestBody UserRO ro) {
        System.out.println("登录");
        return processData(() -> userService.login(ro), this::processException);
    }

    /**
     * 申请成为授课专家
     */
    @PostMapping(value = "/expertApply")
    @ApiOperation(value = "申请成为授课专家")
    public ResponseEntity<OR<LoginVO>> expertApply(@RequestBody UserRO ro) {
        System.out.println("登录");
        return processData(() -> userService.login(ro), this::processException);
    }

    /**
     * 上传头像
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadAvatar")
    public ResponseEntity<OR<String>> uploadAvatar(@RequestParam("avatar") MultipartFile file) {
        String userId = currentUser();
        return processData(() -> userService.uploadAvatar(file, userId), this::processException);
    }

    @GetMapping("/userInfo/{account}")
    public ResponseEntity<OR<UserVO>> findOne(@PathVariable String account) {
        return processData(()->userService.getOne(account),this::processException);
    }
}
